package com.medical.medicalmodulecommon.response;

import com.medical.medicalmodulecommon.response.CommonResponse;
import com.medical.medicalmodulecommon.response.ResponseCode;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/test/response")
public class CommonResponseTestController {

    /**
     * 测试成功响应 - 无数据
     */
    @GetMapping("/success")
    public CommonResponse<String> testSuccess() {
        return CommonResponse.createForSuccess();
    }

    /**
     * 测试成功响应 - 带消息
     */
    @GetMapping("/success-message")
    public CommonResponse<String> testSuccessMessage() {
        return CommonResponse.createForSuccessMessage("操作成功完成");
    }

    /**
     * 测试成功响应 - 带数据
     */
    @GetMapping("/success-data")
    public CommonResponse<Map<String, Object>> testSuccessData() {
        Map<String, Object> user = new HashMap<>();
        user.put("id", 1);
        user.put("name", "张三");
        user.put("email", "zhangsan@example.com");
        return CommonResponse.createForSuccess(user);
    }

    /**
     * 测试成功响应 - 带消息和数据
     */
    @GetMapping("/success-message-data")
    public CommonResponse<Map<String, Object>> testSuccessMessageData() {
        Map<String, Object> product = new HashMap<>();
        product.put("id", 1001);
        product.put("name", "笔记本电脑");
        product.put("price", 5999.00);
        return CommonResponse.createForSuccess("商品信息获取成功", product);
    }

    /**
     * 测试错误响应 - 默认错误
     */
    @GetMapping("/error-default")
    public CommonResponse<String> testErrorDefault() {
        return CommonResponse.createForError();
    }

    /**
     * 测试错误响应 - 自定义错误消息
     */
    @GetMapping("/error-custom")
    public CommonResponse<String> testErrorCustom() {
        return CommonResponse.createForError("数据库连接失败");
    }

    /**
     * 测试权限不足响应
     */
    @GetMapping("/limits")
    public CommonResponse<String> testLimits() {
        return CommonResponse.createForLimits();
    }

    /**
     * 测试权限不足 - 自定义消息
     */
    @GetMapping("/limits-custom")
    public CommonResponse<String> testLimitsCustom() {
        return CommonResponse.createForLimits("您没有删除权限");
    }

    /**
     * 测试通用条件方法 - 根据布尔值返回
     */
    @GetMapping("/condition/{result}")
    public CommonResponse<String> testCondition(@PathVariable boolean result) {
        return CommonResponse.createForCondition(result, "操作成功", "操作失败");
    }

    /**
     * 测试通用条件方法 - 带数据
     */
    @GetMapping("/condition-data/{id}")
    public CommonResponse<Map<String, Object>> testConditionData(@PathVariable int id) {
        // 模拟根据ID查询用户
        Map<String, Object> user = new HashMap<>();
        user.put("id", id);
        user.put("name", "用户" + id);

        boolean userExists = id > 0 && id < 100; // 模拟用户存在条件

        return CommonResponse.createForCondition(userExists, user, "用户不存在");
    }

    /**
     * 测试快速创建方法 - of(ResponseCode)
     */
    @GetMapping("/of/enum")
    public CommonResponse<String> testOfEnum() {
        return CommonResponse.of(ResponseCode.SUCCESS);
    }

    /**
     * 测试快速创建方法 - of(ResponseCode, message)
     */
    @GetMapping("/of/enum-message")
    public CommonResponse<String> testOfEnumMessage() {
        return CommonResponse.of(ResponseCode.ERROR, "文件上传失败");
    }

    /**
     * 测试快速创建方法 - of(ResponseCode, data)
     */
    @GetMapping("/of/enum-data")
    public CommonResponse<Map<String, Object>> testOfEnumData() {
        Map<String, Object> data = new HashMap<>();
        data.put("total", 150);
        data.put("page", 1);
        data.put("size", 20);
        return CommonResponse.of(ResponseCode.SUCCESS, data);
    }

    /**
     * 测试状态判断方法 - 模拟业务场景
     */
    @GetMapping("/user/{id}/permission")
    public CommonResponse<Map<String, Object>> testUserPermission(@PathVariable int id) {
        Map<String, Object> result = new HashMap<>();
        result.put("userId", id);
        result.put("hasPermission", id % 2 == 0); // 偶数ID有权限

        CommonResponse<Map<String, Object>> response = CommonResponse.createForSuccess(result);

        // 测试状态判断方法
        System.out.println("是否成功: " + response.isSuccess());
        System.out.println("是否错误: " + response.isError());
        System.out.println("是否权限不足: " + response.isLimits());

        return response;
    }

    /**
     * 综合测试 - 模拟真实业务场景
     */
    @PostMapping("/simulate-operation")
    public CommonResponse<Map<String, Object>> simulateOperation(@RequestBody Map<String, String> request) {
        String operation = request.get("operation");
        String userRole = request.get("role");

        // 模拟权限检查
        if (!"admin".equals(userRole)) {
            return CommonResponse.createForLimits("需要管理员权限");
        }

        // 模拟操作执行
        boolean success = "create".equals(operation) || "update".equals(operation);

        if (success) {
            Map<String, Object> data = new HashMap<>();
            data.put("operation", operation);
            data.put("timestamp", System.currentTimeMillis());
            data.put("id", 1001);
            return CommonResponse.createForSuccess("操作执行成功", data);
        } else {
            return CommonResponse.createForError("不支持的操作类型: " + operation);
        }
    }
}